﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MoetutuServer.Helper
{
    class FileHelper
    {
        /// <summary>
        /// 判断文件是否存在，存在返回true
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="CreateFlag"></param>
        /// <returns></returns>
        public static bool FileExits(String FilePath, bool CreateFlag = false)
        {

            if (!File.Exists(FilePath))
            {
                if (CreateFlag)
                {
                    try
                    {
                        File.AppendAllText(FilePath, "",Encoding.UTF8);
                        return true;
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                        PrintLog.Log(FilePath);
                        return false;
                    }
                }
                else
                { return false; }
            }

            else
                return true;


        }
        /// <summary>
        /// 用utf8读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadContextUtf8(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8))
                {
                    string context = sr.ReadToEnd();
                    return context;
                }
            }

        }

        /// <summary>
        /// 判断文件夹是否存在
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="CreatFlag"></param>
        /// <returns></returns>
        public static bool FloderExits(String FloderPath, bool CreatFlag)
        {
            if (!Directory.Exists(FloderPath))
            {

                if (!CreatFlag)
                {
                    return false;
                }
                Console.WriteLine("已创建" + FloderPath);
                Directory.CreateDirectory(FloderPath);
            }
            return true;
        }
        /// <summary>
        /// 获取文件名列表，不带路径
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public static List<string> GetFileName(String FloderPath, String Pattern = "*.*") {

            List<String> FileList = new List<String>();
            try
            {
                foreach (string line in Directory.GetFiles(FloderPath, Pattern))
                    FileList.Add(Path.GetFileName(line));
                 
            }
            catch (Exception ex)
            {
                PrintLog.Log("当前文件夹没有创建。");
                PrintLog.Log(ex);
            }
            return FileList;
        }
        /// <summary>
        /// 获取目录下文件列表，带路径
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public static List<String> GetFullFileNameList(String FloderPath, String Pattern = "*.*")
        {

            List<String> FileList = new List<String>();
            try
            {
                FileList = Directory.GetFiles(FloderPath, Pattern).ToList<String>();
            }
            catch (Exception ex)
            {
                PrintLog.Log("当前文件夹没有创建。");
                PrintLog.Log(ex);
            }
            return FileList;
        }
        /// <summary>EAD
        /// 统一写入utf8的方法
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Content"></param>
        public static void AppendUTF8Text(String FilePath, String Content)
        {
            File.AppendAllText(FilePath, "\r\n" + Content, Encoding.UTF8);

        }
        /// <summary>
        /// 统一写入utf8的方法
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Content"></param>
        public static void AppendUTF8List(String FilePath, List<String> Content)
        {
            File.AppendAllText(FilePath, "\r\n", Encoding.UTF8);
            File.AppendAllLines(FilePath, Content, Encoding.UTF8);

        }
        /// <summary>
        /// 覆盖源文件，写入list
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Content"></param>
        public static void WriteUTF8List(String FilePath, List<String> Content)
        {
            try
            {
                File.WriteAllLines(FilePath, Content, Encoding.UTF8);
            }
            catch (Exception ex) {
                PrintLog.Log(ex);
            }
        }
        /// <summary>
        /// 读取所有行
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static List<string> ReadAllLine(String FilePath) {
            List<String> ReadAllLine = new List<string>();
            try
            {
                if (FileHelper.FileExits(FilePath))
                    ReadAllLine = File.ReadAllLines(FilePath).ToList<string>();
                else {

                    Console.WriteLine("文件不存在");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return ReadAllLine;
        }
        /// <summary>
        /// 去重复行和空行，然后重新覆盖这个文件
        /// </summary>
        public static void ToRepartLine(String FilePath)
        {
            List<String> AllLine = ReadAllLine(FilePath);

            File.Copy(FilePath, FilePath + ".bak", true);
            foreach (String line in File.ReadAllLines(FilePath))
            {

                if (line != "" && (!AllLine.Contains(line)))
                {
                    AllLine.Add(line.Trim());
                    Console.WriteLine("已读取" + line);
                }
            }
            File.WriteAllLines(FilePath, AllLine);
        }
        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="NewFilePath"></param>
        public static void FileMove(String FilePath, String NewFilePath)
        {
            try
            {
                FloderExits(Path.GetDirectoryName(NewFilePath), true);
                if (File.Exists(NewFilePath))
                    File.Delete(NewFilePath);
                File.Move(FilePath, NewFilePath);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 删除文件的方法，会告诉你删除是否成功
        /// </summary>
        /// <param name="Filename"></param>
        /// <returns></returns>
        public static bool FileDelete(String Filename)
        {
            try
            {
                File.Delete(Filename);
                return true;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                return false;
            }
        }

        /// <summary>
        /// 判断文件大小是否超过了界限，超过返回false
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="FileSize">文件大小 KB</param>
        /// <returns></returns>
        public static bool JudgementOfSize(String FileName, int FileSize)
        {

            FileInfo fi = new FileInfo(FileName);
            if (fi.Length > 1024 * FileSize)
            {

                return false;
            }
            return true;
        }
        /// <summary>
        /// 每次启动程序清理缓存
        /// </summary>
        /// <param name="FloderPath"></param>
        public static void DeleteAllChildrenFile(String FloderPath) {

            foreach (string line in GetFullFileNameList(FloderPath)) {

                FileDelete(line);
            }
        }
        /// <summary>
        /// 修改文件md5
        /// </summary>
        /// <param name="Filename"></param>
        public static void ModifyMD5(String Filename)
        {

            if (!File.Exists(Filename))
            {
                Console.WriteLine("文件不存在");
                return;
            }
            else
            {
                //转化byte[]到list 
                List<byte> ByteList = File.ReadAllBytes(Filename).ToList<byte>();
                ///添加一个二进制随机数
                ByteList.AddRange(Encoding.Default.GetBytes(Convert.ToString(new Random().Next(), 2)).ToList<byte>());
                try
                {
                    File.WriteAllBytes(Filename, ByteList.ToArray());
                }
                catch (Exception ex)
                {

                    PrintLog.Log(ex);
                };

            }


        }
    }
}
